package com.caih.contacts.service.impl;

import com.caih.contacts.dao.EmployeesDao;
import com.caih.contacts.entity.Employees;
import com.caih.contacts.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class EmployessServiceimpl implements com.caih.contacts.service.EmployessService {


   @Autowired
    private EmployeesDao employeesDao;


    //创建新成员
    @Override
    public Result<Employees> save(Employees employees) {
        employeesDao.save(employees);
        Result<Employees> result = new Result<>();
        result.setData(employees);
        result.setCode(Result.OK);
        result.setMessage("保存成功");
        return  result;
    }

    //更新成员信息
    @Override
    public Result<Employees> update(Employees employees) {
        Employees employees1 =  employeesDao.save(employees);
        Result<Employees> result = new Result<>();
        result.setMessage("更新成功");
        result.setCode(Result.OK);
        result.setData(employees1);
        return result;
    }

    //查询全部成员信息
   @Override
    public Result<List<Employees>> queryAll() {
        List<Employees> list = employeesDao.findAll();
        Result<List<Employees>> result = new Result<>();
        if (list.size()>0){
            result.setCode(Result.OK);
            result.setData(list);
            result.setMessage("查询成功");
        }else {
            result.setCode(Result.ERROR);
            result.setMessage("查询无结果");
        }
        return result;
    }


    //userid查询成员信息
    @Override
    public Result<Employees> queryById(String id) {
        Result<Employees> result = new Result<>();
        Employees employees = employeesDao.findByUserId(id);
        if(employees!=null){
            result .setMessage("查询成功");
            result.setCode(Result.OK);
            result.setData(employees);
        }else{
            result.setCode(Result.ERROR);
            result.setMessage("查询无结果");
        }
        return result;
    }



    /**
     * 通过userId删除成员
     * @param id
     * @return
     */
    @Override
    public Result<String> deleteById(String id) {
        employeesDao.deleteByUserId(id);
        Result<String> result = new Result<>();
        result.setMessage("删除");
        result.setCode(Result.OK);
        return result;
    }


    //通过名字name查询
    @Override
    public Result<Employees> queryByName(String name) {
        Result<Employees> result = new Result<>();
        Employees employees = employeesDao.findByName(name);
        if(employees!=null){
            result.setMessage("查询成功");
            result.setCode(Result.OK);
            result.setData(employees);
        }else{
            result.setCode(Result.ERROR);
            result.setMessage("查询无结果");
        }
        return result;

    }

    //通过电话tel查询
    @Override
    public Result<Employees> queryByMobile(String mobile) {
        Result<Employees> result = new Result<>();
        Employees employees = employeesDao.findByMobile(mobile);
        if (employees!=null){
            result .setMessage("查询成功");
            result.setCode(Result.OK);
            result.setData(employees);
        }else{
            result.setCode(Result.ERROR);
            result.setMessage("查询无结果");
        }
        return result;
    }
    //通过部门id查询部门下的职员
    @Override
    public Result<List<Employees>> queryAllByDepartment(Integer department) {
        List<Employees> list=employeesDao.queryAllByDepartment(department);
        Result<List<Employees>> result = new Result<>();

        if (list.size()>0){
            result.setCode(Result.OK);
            result.setData(list);
            result.setMessage("查询成功");
        }else {
            result.setCode(Result.ERROR);
            result.setMessage("查询无结果");
        }
        return result;

    }
    /*
    * 批量删除
    * */
    public Result<String> batchdelete(List<String> useridList){
        return null;
    }

}
